/**
 * 资产数据汇总
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.get('/', function(req, res, next) {

    let role = _.trim(String(req.query.role));
    let departid = _.trim(String(req.query.departid));

    let data = {
        isSuccess: false,
        data: {},
        msg: '资产汇总数据获取失败！'
    };
   console.log("ceshi ")
   /**
    * 首页图表数据
    * 需要根据用户角色和所属部门来确定返回的数据
    * 角色为1/2时，需要按其所属部门对数据进行限制
    * 其他角色则不进行限制
    */
   let whereClause = "";
   if(Number(role) == 1 || Number(role)==2) {
     whereClause = " and t1.departid=" + departid + " ";
   }
    let sql = "select count(state) count, t2.name text from equipmentinfo t1 right join sys_equipment_state t2 on t1.state = t2.id " + whereClause + "group by t2.name;";
    let sql2 = "select count(depart) count, t2.name text FROM equipmentinfo t1 right join sys_depart t2 ON t1.departid = t2.id " + whereClause + "Group by t2.name;";
    let sql3 = "select count(type)  count, t2.name text from equipmentinfo t1 right join sys_equipment_type t2 on t1.type = t2.name "+ whereClause +" Group by t2.name;";
   console.log("sql1:" + sql)
   console.log("sql2:" + sql2)
   console.log("sql3:" + sql3)
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            data.data['state'] = rows;
            db.execute(sql2, function(err1, rows1) {
                if(err1) {
                    return res.json(data);
                } else {
                    data.data['depart'] = rows1;
                    db.execute(sql3, function(err2, rows2) {
                        if(err1) {
                            return res.json(data);
                        } else {
                            data.data['type'] = rows2;
                            data.isSuccess = true;
                            data.msg = "资产汇总数据获取成功！";
                            console.log(data);
                            return res.json(data);
                        }
                    })
                }
            })
        }
    })
});
module.exports = router;